const doio = require('doio');
const logger = require('doio-logger');
const monitor = require('doio-monitor');
const doioloader = require('doio-loader');
const gohttp = require('gohttp');

class ants {

  constructor (options = null) {

    this.options = {
      doio:{
        debug: true
      },
      
      logger: {},

      monitor: {
        type: 'text',
        file: 'doio-monitor.log'
      },

      loader:{}

    };

    if (!options && typeof options !== 'object') {
      this.options = options;
    }

    if (options && typeof options === 'object') {
      this.setOptions('doio', options);
      this.setOptions('loader', options);
      this.setOptions('monitor', options);
      this.setOptions('logger', options);
    }

    this.doio = new doio(this.options.doio);

    this.router = this.doio.router;

    this.logger = null;

    if (this.options.logger !== undefined && this.options.logger !== false) {
      this.logger = new logger(this.options.logger);
      this.logger.init(this.doio);
    }

    this.monitor = null;

    if (this.options.monitor !== undefined && this.options.monitor !== false) {
      this.monitor = new monitor(this.options.monitor);
      this.monitor.init(this.doio);
    }

    this.loader = null;
    if (this.options.loader !== undefined && this.options.loader !== false) {
      this.loader = new doioloader(this.options.loader);
      this.loader.init(this.doio);
    }

  }

  setOptions (key, opts) {
    if (!opts || typeof opts !== 'object') {
      return;
    }

    if (opts[key] === undefined || opts[key] === null || typeof opts[key] !== 'object') {
      return;
    }

    for (let k in opts[key]) {
      this.options[key][k] = opts[key][k];
    }

  }

  debug (b = true) {
    this.doio.config.debug = b;
    return this;
  }

  start (port = 2022, host = '0.0.0.0', workers = 0) {
    this.doio.printRoute();
    return this.doio.daemon(port, host, workers);
  }

}

module.exports = {
  ants,
  doio,
  logger,
  monitor,
  loader : doioloader,
  http: gohttp
};
